package model.micro
{
	import model.macro.RangedWeapon;
	import model.macro.Unit;
	import model.micro.projectiles.Projectile;
	import util.Random;
	import util.Settings;
	
	/**
	 * A group of soldiers with ranged weapons.
	 */
	public class RangedFieldUnit extends FieldUnit
	{
		
		// TODO: Make analogous to ArcherUnit
		
		/** In radains CCW from positive x axis */
		private var aim_direction:Number;
		/** A list of the number of seconds */
		private var wait_times:Vector.<Number>;
		/** Number of soldiers that can currently shoot */
		private var can_shoot:int;
		
		/**
		 * Create a new ranged field unit.
		 * @param	unit
		 * @param	x
		 * @param	battle
		 * @param	team
		 */
		public function RangedFieldUnit(unit:Unit, x:int, battle:Battle)
		{
			super(unit, x, battle);
			aim_direction = 0;
			
			wait_times = new Vector.<Number>();
			for (var i:int = 0; i < unit.number_of_soldiers; i++)
			{
				wait_times.push(0.0);
			}
		}
		
		/**
		 * Have all archers which have reloaded fire.
		 */
		public function shoot():void
		{
			for (var i:int = 0; i < wait_times.length; i++)
			{
				if (wait_times[i] <= 0)
				{
					var weapon:RangedWeapon = unit.weapon as RangedWeapon;
					var speed:Number = Random.normal(weapon.release_velocity, 1);
					var x:Number = Random.uniform(bounding_box.left, bounding_box.right);
					var vx:Number = speed * Math.cos(aim_direction);
					var vy:Number = speed * Math.sin(aim_direction);
					battle.addProjectile(new Projectile(x, bounding_box.top + 10, vx, vy, weapon.projectile_type));
					
					wait_times[i] = Random.normal((unit.weapon as RangedWeapon).reload_time, (unit.weapon as RangedWeapon).reload_time/10);
					
					can_shoot--;
				}
			}
		}
		
		/**
		 * Called every frame.
		 *
		 * Calculates the number of soldiers that can shoot.
		 */
		override public function update():void
		{
			super.update();
			
			can_shoot = 0;
			for (var i:int = wait_times.length - 1; i >= 0; i--)
			{
				if (wait_times[i] > Settings.getTimeStep())
				{
					wait_times[i] -= Settings.getTimeStep();
				}
				else
				{
					wait_times[i] = 0;
					can_shoot++;
				}
			}
		}
		
		/**
		 * Called when attacked.
		 * @param	damage
		 */
		override public function attacked(damage:Number):void
		{
			super.attacked(damage);
			
			while (wait_times.length > unit.number_of_soldiers && unit.number_of_soldiers > 0)
			{
				wait_times.pop();
			}
		}
		
		override public function onHit(projectile:Projectile):void
		{
			super.onHit(projectile);
			
			while (unit.number_of_soldiers > 0 && wait_times.length > unit.number_of_soldiers)
			{
				wait_times.pop();
			}
		}
		
		/**
		 * @return	The number of archers in this unit that are ready to fire.
		 */
		public function getCanShoot():int
		{
			return can_shoot;
		}
		
		/**
		 * @return	The aiming direction
		 */
		public function getAimDirection():Number
		{
			return aim_direction;
		}
		
		/**
		 * Set the aiming direction
		 * @param	value
		 */
		public function setAimDirection(value:Number):void
		{
			aim_direction = value;
		}
	
	}

}